package groups;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import misc.misc;

import database.databaseInteraction;
import database.database_auth;

/** handling of groups
 * 
 * currently a group has this format
 * ID 	GROUPID 	NAME 	USERID 	DESCRIPTION 	OWNER 
 * **/
public class groups {
	String ID = "";
	String GROUPID = "";
	String NAME = "";
	String USERID = "";
	String DESCRIPTION = "";
	String OWNERID = "";

	static databaseInteraction DatabaseConnection = new databaseInteraction();

	static misc misc_inst = new misc();
	
	/**
	 * creates a new group
	 */
	public String newGroup(String NAME, String OWNERID, String DESCRIPTION, database_auth database_auth_inst)
	{
		String status = "";

		String question = "INSERT INTO `together`.`groups` (`ID`, `NAME`, `USERID`, `DESCRIPTION`, `OWNERID`) VALUES (NULL, '"+NAME+"', "+OWNERID+", '"+DESCRIPTION+"', '"+OWNERID+"');";

		try
		{
			DatabaseConnection.askDatabase(database_auth_inst, question); // StringBuffer answer = DatabaseConnection.askDatabase( host, DatabaseName, TableName, user, password, question);
			
			if(DatabaseConnection.success)
			{
				status = "group successfuly created";
			}
			else
			{
				status = "failed to create group";
			}
		}
		catch(Exception e)
		{
			status = e.toString();
		}
		
		return status;
	}

	/**
	 * get all members (USERIDs) of a specific groups of a user
	 * STATUS: UNTESTED
	 */
	public ArrayList getUsersOfGroupName(String GROUPNAME, String OWNERID, database_auth database_auth_inst)
	{
		ArrayList<String> groupMembers = new ArrayList<String>();
		
		HashMap groups = getGroups(OWNERID, database_auth_inst);
		
		Set<String> KEYSET = groups.keySet();
		
		for ( Object OBJECT : KEYSET )
		{
			if(OBJECT.equals(GROUPNAME))
			{
				groupMembers = (ArrayList<String>) groups.get(OBJECT);
				break;
			}
		}
		
		return groupMembers;
	}
	
	/**
	 * get all members (USERIDs) of a specific groups of a user
	 * STATUS: UNTESTED
	 */
	public ArrayList getUsersOfGroupID(String GROUPID, String OWNERID, database_auth database_auth_inst)
	{
		ArrayList<String> groupMembers = new ArrayList<String>();
		
		HashMap groups = getGroups(OWNERID, database_auth_inst);
		
		Set<String> KEYSET = groups.keySet();
		
		for ( Object OBJECT : KEYSET )
		{
			if(OBJECT.equals(GROUPID))
			{
				groupMembers = (ArrayList<String>) groups.get(OBJECT);
				break;
			}
		}
		
		return groupMembers;
	}
	
	/**
	 * get all the members (USERIDs) of a all groups of a user
	 * and returns it in this format:
		{friends=
			[
				{NAME=friends, DESCRIPTION=users i wish to remember and chat with often, GROUPID=5, OWNERID=1, ID=5, USERID=1}
				... here come all entries that have the GROUPNAME "friends"
			],
		
		test=
			[
				{NAME=test, DESCRIPTION=, GROUPID=1, OWNERID=1, ID=1, USERID=1},
				{NAME=test, DESCRIPTION=, GROUPID=0, OWNERID=1, ID=6, USERID=1}
				... here come all entries that have the GROUPNAME "test"
			],
		
		family=
			[
				{NAME=family, DESCRIPTION=users of my family, GROUPID=3, OWNERID=1, ID=3, USERID=1}
				... here come all entries that have the GROUPNAME "family"
			],
		
		blockedUser=
			[
				{NAME=blockedUser, DESCRIPTION=users i wish to ignore, GROUPID=4, OWNERID=1, ID=4, USERID=1}
				... here come all entries that have the GROUPNAME "blockedUser"
			]
		}
	 */
	public HashMap getGroups(String OWNERID, database_auth database_auth_inst)
	{
		HashMap result = new HashMap();
		
		String question = "SELECT * FROM `groups` WHERE `OWNERID` = "+OWNERID+";"; // mysql function to generate passsword hash: SELECT PASSWORD('djfkldjfkld!');
		
		DatabaseConnection.askDatabase(database_auth_inst, question); // StringBuffer answer = DatabaseConnection.askDatabase( host, DatabaseName, TableName, user, password, question);
		
		ArrayList<Object> groups = (ArrayList<Object>) DatabaseConnection.answerJAVA.clone();
		
		// 1. get a list of unique GROUPNAMES
		ArrayList<String> groupNames = misc_inst.unique(DatabaseConnection.answerJAVA,"NAME");
		
		// 2. create a HashMap groupname = ArrayList (with list of USERIDs that belong to that group)
		for ( Object groupName : groupNames )
		{
			ArrayList<Object> group = new ArrayList<Object>();
			
			// check if allready in the list
			if(result.get(groupName) == null)
			{
				for ( Object item : DatabaseConnection.answerJAVA )
				{
					if(item instanceof HashMap)
					{
						HashMap item1 = (HashMap) item;
						
						if(groupName.equals(item1.get("NAME")))
						{
							group.add(item);
						}
					}
				}
			}
			
			result.put(groupName, group);
		}
		
		return result;
	}
	
	/** get all groupIDs that the user is member of **/
	public ArrayList<Object> UserIsMemberOfGroups(String USERID, database_auth database_auth_inst)
	{
		String question = "SELECT * FROM `groups` WHERE `USERID` = "+USERID+";"; // mysql function to generate passsword hash: SELECT PASSWORD('djfkldjfkld!');
		
		DatabaseConnection.askDatabase(database_auth_inst, question); // StringBuffer answer = DatabaseConnection.askDatabase( host, DatabaseName, TableName, user, password, question);
		
		ArrayList<Object> groups = (ArrayList<Object>) DatabaseConnection.answerJAVA.clone();
		
		return groups;
	}
	
}
